package fleditor.view
{
	import flash.events.MouseEvent;
	
	import fleditor.common.WindowView;
	import fleditor.controller.FlEditorController;
	import fleditor.events.FlEditorEvent;
	import fleditor.model.FlEditorModel;
	import fleditor.vo.MapVO;
	
	import ui.createMapFrameUI;
	
	public class CreateMapView extends WindowView
	{
		private var _controller:FlEditorController;
		private var _createFrame:createMapFrameUI;
		
		public function CreateMapView(controller:FlEditorController)
		{
			super(445, 320, "地图属性");
			_controller=controller;
			_createFrame=new createMapFrameUI();
			addChild(_createFrame);
			
			_createFrame.btnOk.buttonMode=_createFrame.btnClose.buttonMode=true;
			
			setEvent();
		}
		
		/**
		 *设置事件 
		 * 
		 */		
		private function setEvent():void
		{
			_createFrame.btnOk.addEventListener(MouseEvent.CLICK, __onConfirmClick);
			_createFrame.btnClose.addEventListener(MouseEvent.CLICK, __onConfirmClick);
		}
		
		
		private function __onConfirmClick(evt:MouseEvent):void
		{
			switch(evt.type)
			{
				case MouseEvent.CLICK:
					switch(evt.currentTarget)
					{
						case _createFrame.btnOk:
							confirmCreate();
							break;
						case _createFrame.btnClose:
							closeView();
							break;			
					}
					break;
			}
		}
		
		/**
		 * 确认创建新地图
		 */		
		private function confirmCreate():void
		{
			var mapVO:MapVO=new MapVO();
			mapVO.mapName=_createFrame.txtName.text;
			mapVO.mapType=_createFrame.rbDiamond.selected ? 0 : _createFrame.rbStaggered.selected ? 1 : _createFrame.rbRectangle.selected ? 2 : 3;
			mapVO.mapDescription=_createFrame.txtDescription.text;
			mapVO.mapRow=int(_createFrame.txtRow.text);
			mapVO.mapCols=int(_createFrame.txtCols.text);
			mapVO.cellSize=int(_createFrame.txtCellSize.text);
			mapVO.mapVisualNumber=int(_createFrame.txtVisualNumber.text);
			
			_controller.createMap(mapVO);
			
			closeView();
		}
		
		/**
		 * 关闭窗口 
		 * 
		 */		
		private function closeView():void
		{
			dispose();
		}
		
		/**
		 * 资源释放 
		 * 
		 */		
		override public function dispose():void
		{
			super.dispose();
			
			if(_createFrame) _createFrame.btnOk.removeEventListener(MouseEvent.CLICK, __onConfirmClick);
			if(_createFrame) _createFrame.btnClose.removeEventListener(MouseEvent.CLICK, __onConfirmClick);
			
			if(_createFrame) _createFrame.parent.removeChild(_createFrame);
			_createFrame=null;
			if(parent) parent.removeChild(this);
		}
	}
}